package cn.fighter3.serv.service.impl;

import cn.fighter3.api.rpc.StockApiService;
import cn.fighter3.api.model.StockAddDTO;
import cn.fighter3.serv.mapper.ShopStockMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import cn.fighter3.serv.entity.ShopStock;
/**
 * @Author 三分恶
 * @Date 2021/11/14
 * @Description 库存服务提供RPC接口实现类
 */
@org.apache.dubbo.config.annotation.Service(cluster = "failfast",loadbalance = "roundrobin")
@Slf4j
public class StockApiServiceImpl implements StockApiService {
    @Autowired
    private ShopStockMapper stockMapper;

    /**
     * 添加库存
     *
     * @param stockAddDTO
     * @return
     */
    @Override
    public Integer addStock(StockAddDTO stockAddDTO) {
        ShopStock stock = new ShopStock();
        stock.setGoodsId(stockAddDTO.getGoodsId());
        stock.setInventory(stockAddDTO.getAccount());
        log.info("准备添加库存,参数:{}", stock.toString());
        this.stockMapper.insert(stock);
        Integer stockId = stock.getStockId();
        log.info("添加库存成功,stockId:{}", stockId);
        return stockId;
    }

    /**
     * 获取库存数量
     *
     * @param goodsId
     * @return
     */
    @Override
    public Integer getAccountById(Integer goodsId) {
        ShopStock stock = this.stockMapper.selectOne(Wrappers.<ShopStock>lambdaQuery().eq(ShopStock::getGoodsId, goodsId));
        Integer account = stock.getInventory();
        return account;
    }
}
